var UI = {};
$(document).ready(
    function(){
        
        (function (UI) {
            
            UI.start = function(){
                $.when(Actions.barStatsAgeLimit({action : "test"}), Actions.barStatsPostalPlaces({action : "test"})).then(
                    function(dataAL, dataPP){
                        var ageLimits = [];
                        var postalPlaces = [];
                        
                        for(var i=0; i<dataAL.length; i++){
                            ageLimits.push([dataAL[i].age_limit, Number(dataAL[i].no_of_bars)]);
                        }
                        for(var i=0; i<dataPP.length; i++){
                            postalPlaces.push([dataPP[i].place, Number(dataPP[i].no_of_bars)]);
                        }
                        
                        // Make monochrome colors and set them as default for all pies
                        Highcharts.getOptions().plotOptions.pie.colors = (function () {
                            var colors = [],
                                base = Highcharts.getOptions().colors[0],
                                i;

                            for (i = 0; i < 10; i += 1) {
                                // Start out with a darkened base color (negative brighten), and end
                                // up with a much brighter color
                                colors.push(Highcharts.Color(base).brighten((i - 3) / 7).get());
                            }
                            return colors;
                        }());

                        // Build the chart
                        $('#pie-age-limit').highcharts({
                            chart: {
                                plotBackgroundColor: null,
                                backgroundColor:'rgba(255, 255, 255, 0.1)',
                                plotBorderWidth: null,
                                plotShadow: false,
                            },
                            title: {
                                text: 'Bar Statistics'
                            },
                            subtitle: {
                                text: 'Age Limit and Posting Place'
                            },
                            tooltip: {
                                pointFormat: '{series.name}: <b>{point.y} Bars</b>'
                            },
                            plotOptions: {
                                pie: {
                                    allowPointSelect: true,
                                    cursor: 'pointer',
                                    dataLabels: {
                                        enabled: true,
                                        format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                                        style: {
                                            color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                                        }
                                    }
                                }
                            },
                            series: [{
                                type: 'pie',
                                name: 'Age Limit',
                                center: ['20%'],
                                data: ageLimits
                            },
                            {
                                type: 'pie',
                                name: 'Postal Place',
                                center: ['80%'],
                                data: postalPlaces
                            }
                            ],
                        });
                    }
                );
            };
            
        }(UI));
        var Helpers = {};
        (function (Helpers) {
            
            
            
        }(Helpers));
        
        var Actions = {};
        (function (Actions) {
            
            Actions.barStatsPostalPlaces = function(args) {
                var dfd = new $.Deferred();
                $.ajax({
                    type: "GET",
                    url: "../resources/helpers/barStatsPostalPlaces.php",
                    data: args,
                    cache: false,
                    success: function(data){
                        data = JSON.parse(data);
                        dfd.resolve(data);
                    }
                });
                return dfd.promise();
            };
            
            Actions.barStatsAgeLimit = function(args) {
                var dfd = new $.Deferred();
                $.ajax({
                    type: "GET",
                    url: "../resources/helpers/barStatsAgeLimit.php",
                    data: args,
                    cache: false,
                    success: function(data){
                        data = JSON.parse(data);
                        dfd.resolve(data);
                    }
                });
                return dfd.promise();
            };
            
        }(Actions));
        
        var Controller = {};
        (function (Controller) {
            
            Controller.init= function(){
                
            };
            
        }(Controller));	
        
        UI.start();
        Controller.init();
    }
);